from datetime import datetime
from django.db import models

class Catalog(models.Model):
    name = models.CharField(max_length = 255)
    slug = models.SlugField(max_length = 150)
    publisher = models.CharField(max_length = 300)
    description = models.TextField()
    pub_date = models.DateTimeField(default = datetime.now)
    
    def __unicode__(self):
        return u'%s' % self.name
    

class CatalogCategory(models.Model):
    catalog = models.ForeignKey('Catalog', related_name = 'categories')
    parent = models.ForeignKey('self', blank = True, null = True,
                               related_name = 'children')
    name = models.CharField(max_length = 300)
    slug = models.CharField(max_length = 150)
    description = models.TextField(blank = True)
    
    def __unicode__(self):
        if self.parent:
            return u'%s: %s - %s' % (self.catalog.name, self.parent, self.name)
        return u'%s: %s' % (self.catalog.name, self.name)
 
class Product(models.Model):
    category = models.ForeignKey('CatalogCategory', related_name = "products")
    name = models.CharField(max_length = 300)
    slug = models.CharField(max_length = 150)
    description = models.TextField()
    photo = models.ImageField(upload_to = 'product_photos', 
                              blank = True)
    manufacturer = models.CharField(max_length = 300,
                                    blank = True) 
    price_in_dollars = models.DecimalField(max_digits = 6,
                                           decimal_places = 2)
    def __unicode__(self):
        return u'%s' % self.name
    
    @models.permalink
    def get_absolute_url(self):
        return ('product_detail', (), {'slug' : self.slug})
   
class ProductDetail(models.Model):
    product = models.ForeignKey('Product', related_name = 'details')
    attribute = models.ForeignKey('ProductAttribute')
    value = models.CharField(max_length = 500)
    description = models.TextField(blank = True)
    
    def __unicode__(self):
        return u'%s : %s - %s' % (self.product, self.attribute, self.value)

class ProductAttribute(models.Model):
    name = models.CharField(max_length = 300)
    description = models.TextField(blank = True)
    
    def __unicode__(self):
        return u'%s' % self.name